<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="资源名称" prop="resourceName">
            <el-input v-model="queryParams.resourceName" placeholder="请输入资源名称" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="资源标识" prop="resourceKey">
            <el-input v-model="queryParams.resourceKey" placeholder="请输入资源标识" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="关联交易" prop="relatedTransaction">
            <el-input v-model="queryParams.relatedTransaction" placeholder="请输入关联交易" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleGetCashier" v-hasPermi="['mall:cashier:get']">取号</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="cashierOccupyInfoList" class="tableEmpty">
        <!--<el-table-column label="推广商" align="center" prop="promoter" />-->
        <el-table-column label="请求号" align="center" prop="requestNo" />
        <el-table-column label="资源名称" align="center" prop="resourceName">
          <template #default="scope">
            <el-link type="danger" :underline="true" @click="handleView(scope.row)">{{ scope.row.resourceName }}</el-link>
          </template>
          <template v-slot:header>
            资源名称
            <el-popover placement="top-start"  effect="dark" :width="260" trigger="hover" content="点击查看资源详情">
              <template #reference>
                <el-icon>
                  <QuestionFilled />
                </el-icon>
              </template>
            </el-popover>
          </template>
        </el-table-column>

        <el-table-column label="资源标识" align="center" prop="resourceKey" />
        <el-table-column label="预售金额" align="center" prop="presaleAmount">
          <template #default="scope">
            <span><el-link type="warning">{{ scope.row.presaleAmount/100 }}</el-link></span>
          </template>
        </el-table-column>
        <el-table-column label="商家名称" align="center" prop="merchantName" />
        <el-table-column label="店铺名称" align="center" prop="shopName" />
        <el-table-column label="销售名称" align="center" prop="sellerName" />
        <el-table-column label="关联交易" align="center" prop="relatedTransaction">
          <template #default="scope">
            <el-link type="success">{{ scope.row.relatedTransaction }}</el-link>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="160px">
          <template #default="scope">
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="到期时间" align="center" prop="expiryTime" width="180">
          <template #default="scope">
            <span>{{ scope.row.expiryTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
          <!--<el-tooltip content="查看" placement="top">
              <el-button link type="primary" @click="handleView(scope.row)" v-hasPermi="['mall:cashier:list']">查看</el-button>
            </el-tooltip>-->
            <el-tooltip content="重新下单" placement="top" v-if="scope.row.relatedTransaction!=null">
              <el-button link type="primary" @click="handleOrder(scope.row)" v-hasPermi="['mall:cashier:order']">重新下单</el-button>
            </el-tooltip>
            <el-tooltip content="创建订单" placement="top" v-if="scope.row.relatedTransaction==null">
              <el-button link type="primary" @click="handleOrder(scope.row)" v-hasPermi="['mall:cashier:order']">创建订单</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改收银员占用信息对话框 -->
    <el-dialog :title="cashierDialog.title" v-model="cashierDialog.visible" width="600px" append-to-body>
      <el-descriptions class="margin-top" :column="1" border>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              <el-icon>
                <iphone />
              </el-icon>
              支付方式
            </div>
          </template>
          <el-input v-model="getCashierForm.paymentWay" placeholder="请输入支付方式" clearable style="width: 240px" />
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              <el-icon>
                <iphone />
              </el-icon>
              预售金额
            </div>
          </template>
          <el-input v-model="getCashierForm.presaleAmount" placeholder="请输入预售金额" clearable style="width: 240px" />
        </el-descriptions-item>
        <!--<el-descriptions-item>
          <template #label>
            <div class="cell-item">
              <el-icon>
                <iphone />
              </el-icon>
              谷歌验证码
            </div>
          </template>
          <el-input v-model="getCashierForm.googleAuthCode" placeholder="请输入谷歌驗證碼" clearable style="width: 240px" />
        </el-descriptions-item>-->
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="getCashierSubmit">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog :title="viewDialog.title" v-model="viewDialog.visible" width="760px" append-to-body>
      <el-descriptions class="margin-top" :column="1" border>
        <el-descriptions-item label-class-name="my-label">
          <template #label>
            <div class="cell-item" style="width:100px">
              资源主鍵
            </div>
          </template>
          {{ selectedRow?.resourceId}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              资源名称
            </div>
          </template>
          {{ selectedRow?.resourceName}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              资源标识
            </div>
          </template>
          {{ selectedRow?.resourceKey}}
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">
              资源内容
            </div>
          </template>
          {{ selectedRow?.resourceValue}}
          <el-image style="width: 80%; height: 80%" :src="selectedRow?.resourceValue" fit="cover"></el-image>
        </el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CashierOccupyInfo" lang="ts">
import { listCashierOccupyInfo, getCashierOccupyInfo, createCashierOrder } from '@/api/promotor/cashierOccupyInfo';
import { CashierOccupyInfoVO, CashierOccupyInfoQuery, CashierOccupyInfoForm } from '@/api/payment/cashierOccupyInfo/types';
import {Iphone, QuestionFilled} from "@element-plus/icons-vue";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const cashierOccupyInfoList = ref<CashierOccupyInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const selectedRow = ref<CashierOccupyInfoVO>();
const getCashierForm = ref({"paymentWay":"","presaleAmount":null,"googleAuthCode":""});

const cashierDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const viewDialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const initFormData: CashierOccupyInfoForm = {
  id: undefined,
  promoter: undefined,
  resourceName: undefined,
  resourceKey: undefined,
  lockTimes: undefined,
  expiryTime: undefined,
  merchant: undefined,
  merchantName: undefined,
  shop: undefined,
  shopName: undefined,
  seller: undefined,
  sellerName: undefined,
  relatedTransaction: undefined,
}
const data = reactive<PageData<CashierOccupyInfoForm, CashierOccupyInfoQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    promoter: undefined,
    resourceName: undefined,
    resourceKey: undefined,
    lockTimes: undefined,
    expiryTime: undefined,
    merchant: undefined,
    merchantName: undefined,
    shop: undefined,
    shopName: undefined,
    seller: undefined,
    sellerName: undefined,
    relatedTransaction: undefined,
    params: {
    }
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询收银员占用信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listCashierOccupyInfo(queryParams.value);
  cashierOccupyInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  cashierDialog.visible = false;
  viewDialog.visible = false;
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 取号按钮操作 */
const handleGetCashier = () => {
  cashierDialog.visible = true;
  cashierDialog.title = "拉取收银员信息";
}

/** 下单 */
const handleOrder = async (row?: CashierOccupyInfoVO) => {
  await proxy?.$modal.confirm('确认要对"' + row?.resourceName + '"收银号进行下单操作？');
  loading.value = true;
  createCashierOrder(row?.id).then(res=>{
    proxy?.$modal.msgSuccess("下单成功");
    getList();
  }).finally(()=>{
    loading.value = false;
  })

}

const handleView = (row?:CashierOccupyInfoVO) => {
  viewDialog.visible = true;
  viewDialog.title = "收银员查看";
  selectedRow.value = row;
}
/** 提交按钮 */
const getCashierSubmit = () => {
  buttonLoading.value = true;
  getCashierOccupyInfo(getCashierForm.value).then(res=>{
    proxy?.$modal.msgSuccess("取号成功");
    cancel();
    getList();
  }).finally(()=>{
    buttonLoading.value = false;
  })
}


/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('payment/cashierOccupyInfo/export', {
    ...queryParams.value
  }, `cashierOccupyInfo_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
